<?php
/**
 * RedXenon Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE.txt
 * A copy of this license can be found in the world-wide-web at this URL:
 * http://www.redxenon.com/license/new-bsd-license.txt
 */

/** File Manager for the template file */
require_once(dirname(dirname(__FILE__))."/File/RXFileManager.php");

/**
 * RedXenon View (RXViewTemplate.php)
 * Defines methods to be applied to templates
 * 
 * @category RedXenon
 * @package RedXenon_View
 * @copyright Copyright (c) 2011 Red Xenon (http://www.redxenon.com)
 * @version $Id: RXViewTemplate.php 1.0 2011-05-02 $ 
 */
class RXViewTemplate{
	
	/**
	 * File Manager for the template File
	 * @var RXFileManager
	 */
	private $templateManager;
	
	/**
	 * Template source code
	 * @var String
	 */
	private $templateSource;
	
	/**
	 * Array that contains the data from the variable in the template
	 * @var Array
	 */
	private $data;
	
	/**
	 * Constructor
	 * @param String $templatePath Path to the template file
	 */
	public function __construct($templatePath){
		$this->templateManager = new RXFileManager();
		$this->data = array();
		try{
			$this->templateManager->setNewFile($templatePath);
			$this->templateSource = $this->templateManager->read();
		}catch(Exception $e){
			throw new Exception("Template could not be read: ".$e->getMessage());
		}
	}
	
	/**
	 * Sets a var in the template
	 * @param String $varName Variable Name
	 * @param mixed $value
	 */
	public function setVar($varName, $value){
		$this->data[$varName] = $value;
	}
	
	/**
	 * Sets a var inside a block in the template
	 * @param String $blockName Block Name
	 * @param String $varName Variable Name inside the Block
	 * @param mixed $value
	 */
	public function setVarBlock($blockName, $varName, $value){		
		$this->data[$blockName][$varName] = $value;
	}
	
	/**
	 * Parses the content of the template with the values contained
	 * in the array data
	 */
	public function parse(){
		self::parseVars();
		self::clean();
		return $this->templateSource;
	}
	
	/**
	 * Parses the blocks in the template
	 */
	private function parseBlocks(){
		foreach($this->data as $elementKey => $element){
			var_dump($element);
			if(is_array($element)){	
				$elementKey = strtoupper($elementKey);
				if(preg_match('/{%'.$elementKey.'[:]*[a-zA-Z0-9_]*%}[.\s\S]*{%'.$elementKey.'[:]*[a-zA-Z0-9_]*%}/', 
				 					  $this->templateSource, $resultMatch)){
				 	preg_match('/{%'.$elementKey.'[:]*[a-zA-Z0-9_]*%}/', $resultMatch[0], $destinationResult);				 	
				 	$destinationVar = str_replace('{%'.$elementKey.':', NULL, $destinationResult[0]);
				 	$destinationVar = str_replace('%}', NULL, $destinationVar);
				 					 	
				  	$content = self::parseVarsBlocks(preg_replace('/{%[a-zA-Z0-9_:]*%}[\r\n|\n]*/', NULL, $resultMatch[0]), $element);
				  	self::setVar($destinationVar, $this->data[$destinationVar].$content);
				}
			}
		}
	}
	
	/**
	 * Parses a Block and stores the content
	 */
	public function parseBlock($blockName){
		$element = $this->data[$blockName];
		if(!$element){
			$element = array(); 
		}
		if(is_array($element)){
			$elementKey = strtoupper($blockName);
			if(preg_match('/{%'.$elementKey.'[:]*[a-zA-Z0-9_]*%}[.\s\S]*{%'.$elementKey.'[:]*[a-zA-Z0-9_]*%}/', 
			 					  $this->templateSource, $resultMatch)){
			 	preg_match('/{%'.$elementKey.'[:]*[a-zA-Z_]*%}/', $resultMatch[0], $destinationResult);				 	
			 	$destinationVar = str_replace('{%'.$elementKey.':', NULL, $destinationResult[0]);
			 	$destinationVar = str_replace('%}', NULL, $destinationVar);
			 					 	
			  	$content = self::parseVarsBlocks(preg_replace('/{%[a-zA-Z0-9_:]*%}[\r\n|\n]*/', NULL, $resultMatch[0]), $element);
			  	if($destinationVar)
			  		self::setVar($destinationVar, $this->data[$destinationVar].$content);
			  	return $content;
			}
		}		
	}
	
	/**
	 * Parses the variables in the template
	 */
	private function parseVars(){
		foreach($this->data as $elementKey => $element){
			if(!is_array($element)){
				$this->templateSource = preg_replace('/[\t]*{'.strtoupper($elementKey).'}[\r\n|\n]*/', $element, $this->templateSource);				
			}
		}
	}
	
	/**
	 * Parses the variables inside a block
	 * @param String $content
	 * @param Array $data
	 */
	private function parseVarsBlocks($content, $data){
		foreach($data as $elementKey => $element){
			if(!is_array($element)){
				$content = preg_replace('/{'.strtoupper($elementKey).'}/', $element, $content);				
			}
		}
		return $content;
	}
	
	/**
	 * Cleans the content of the template
	 */
	private function clean(){
		$this->templateSource = preg_replace('/[\r\n|\n|\t]*{%[a-zA-Z0-9_:]*%}[.\s\S]*{%[a-zA-Z0-9_:]*[\r\n|\n|\t]*%}/', NULL, $this->templateSource);
		$this->templateSource = preg_replace('/[\t]*{[a-zA-Z0-9_]*}[\r\n|\n|\t]*/', NULL, $this->templateSource);
	}
}